﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

using System;
using System.Globalization;

namespace Tesla.Graphics {
    /// <summary>
    /// Represents a vertex element in a vertex buffer.
    /// </summary>
    public struct VertexElement {
        /// <summary>
        /// Semantic name that this element is bound to.
        /// </summary>
        public VertexSemantic SemanticName;

        /// <summary>
        /// Semantic index this element is bound to.
        /// </summary>
        public int SemanticIndex;

        /// <summary>
        /// Format of this element.
        /// </summary>
        public VertexFormat Format;

        /// <summary>
        /// Offset in bytes from the start of the vertex data.
        /// </summary>
        public int Offset;



        /// <summary>
        /// Creates a new instance of <see cref="VertexElement"/>.
        /// </summary>
        /// <param name="semantic">The element's shader semantic.</param>
        /// <param name="semanticIndex">The element's shader semantic index</param>
        /// <param name="format">The element's format.</param>
        /// <param name="offset">The element's offset from the start of the vertex data.</param>
        public VertexElement(VertexSemantic semantic, int semanticIndex, VertexFormat format, int offset) {
            Offset = offset;
            SemanticName = semantic;
            SemanticIndex = semanticIndex;
            Format = format;
        }

        /// <summary>
        /// Determines whether the specified <see cref="System.Object"/> is equal to this instance.
        /// </summary>
        /// <param name="obj">The <see cref="System.Object"/> to compare with this instance.</param>
        /// <returns>
        ///   <c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.
        /// </returns>
        public override bool Equals(object obj) {
            if(obj is VertexElement) {
                VertexElement b = (VertexElement) obj;
                return (this.SemanticIndex == b.SemanticIndex) && (this.SemanticName == b.SemanticName) 
                        && (this.Offset == b.Offset) && (this.Format == b.Format);
            }
            return false;
        }

        /// <summary>
        /// Tests equality between two VertexElements.
        /// </summary>
        /// <param name="a">First VertexElement</param>
        /// <param name="b">Second VertexElement</param>
        /// <returns>True if equal, false otherwise</returns>
        public static bool operator ==(VertexElement a, VertexElement b) {
            return (a.SemanticIndex == b.SemanticIndex) && (a.SemanticName == b.SemanticName) 
                && (a.Offset == b.Offset) && (a.Format == b.Format);
        }

        /// <summary>
        /// Tests inequality between two VertexElements.
        /// </summary>
        /// <param name="a">First VertexElement</param>
        /// <param name="b">Second VertexElement</param>
        /// <returns>True if not equal, false otherwise</returns>
        public static bool operator !=(VertexElement a, VertexElement b) {
            return (a.SemanticIndex != b.SemanticIndex) || (a.SemanticName != b.SemanticName) 
                || (a.Offset != b.Offset) || (a.Format != b.Format);
        }

        /// <summary>
        /// Returns a hash code for this instance.
        /// </summary>
        /// <returns>A hash code for this instance.</returns>
        public override int GetHashCode() {
            int hash = SemanticName.GetHashCode();
            hash = ((hash << 3) - hash) + SemanticIndex.GetHashCode();
            hash = ((hash << 3) - hash) + Format.GetHashCode();
            hash = ((hash << 3) - hash) + Offset.GetHashCode();
            return hash;
        }

        /// <summary>
        /// Returns a <see cref="System.String"/> that represents this instance.
        /// </summary>
        /// <returns>A <see cref="System.String"/> that represents this instance.</returns>
        public override String ToString() {
            return String.Format(CultureInfo.CurrentCulture, "{{SemanticName: {0}, SemanticIndex: {1}, Format: {2}, Offset: {3}}}", new object[] {this.SemanticName, this.SemanticIndex, this.Format, this.Offset});
        }
    }
}
